package com.blqy.fr.template.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.blqy.fr.model.template.TemplateTagRelation;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface TemplateTagRelationMapper extends BaseMapper<TemplateTagRelation> {

    @Select("SELECT template_tag_id FROM template_tag_relation WHERE template_id = #{templateId} AND is_deleted = 0")
    List<Long> selectActiveTagIdsByTemplateId(@Param("templateId") Long templateId);

    @Update("UPDATE template_tag_relation SET is_deleted = 1 WHERE template_id = #{templateId} AND template_tag_id = #{tagId}")
    void softDeleteByTemplateAndTag(@Param("templateId") Long templateId, @Param("tagId") Long tagId);

    @Update("""
        UPDATE 
            template_tag_relation 
        SET 
            is_visible = #{isVisible}, 
            is_required = #{isRequired},
            is_edit = #{isEdit},
            is_deleted = 0
        WHERE 
            template_id = #{templateId} AND template_tag_id = #{templateTagId}
        """)
    int updateByTemplateAndTag(TemplateTagRelation relation);

    @Insert("""
        INSERT INTO 
            template_tag_relation (template_id, template_tag_id, is_visible, is_required, is_edit)
        VALUES 
            (#{templateId}, #{templateTagId}, #{isVisible}, #{isRequired}, #{isEdit})
        """)
    int insert(TemplateTagRelation relation);
}
